﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;

namespace Ccit.OA.Question
{
    public partial class qst_statistics_othertree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ViewState["Query"] = "";
                _hidsRecId.Value = (Request.QueryString["Id"] != null) ? Request.QueryString["Id"].ToString() : "";
                _hidxRecId.Value = (Request.QueryString["xId"] != null) ? Request.QueryString["xId"].ToString() : "";
            }
            initializePage();
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void initializePage()
        {
            createTree(); 
            trvDepAddress.ExpandLevel = 1;
            //int iIndex = int.Parse(_hidxRecId.Value) - 1;
            //trvDepAddress.SelectedNodeIndex = "0." + iIndex;
        }

        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法
            createRootNode(ref oRoot);
            ///添加第一层节点
            trvDepAddress.Nodes.Add(oRoot);
            ///添加节点信息
            createSubNodes(ref oRoot);
        }
        /// <summary>
        /// 创建根节点
        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        { 
            oRoot.Text = "所有意见";
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            string sParam = "Id=" + _hidsRecId.Value;
            string encodeParam = Base64Lib.Encode(sParam);
            oRoot.NavigateUrl = "qst_statistics_otherview.aspx?" + encodeParam;
            oRoot.Target = "iFrmEdit"; 
            oRoot.ID = "0_0";
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        { 
            if(_hidsRecId.Value!="")
                ViewState["Query"] = "and $$qId$$="+_hidsRecId.Value+"";
            QuestionBLL Bll = new QuestionBLL();
            string strAddrXML = Bll.GetOptionsMes(ViewState["Query"].ToString());   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string deptId = "";
            string deptName = ""; 
            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                deptId = eAddr.Element("DEPTID").Value;
                deptName = eAddr.Element("DEPTNAME").Value; 
                oTreeNode = createTreeNode(deptName, deptId);
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string deptName, string deptId)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();
            oNode.Text = deptName;
            string sParam = "xId=" + deptId;
            string encodeParam = Base64Lib.Encode(sParam);
            oNode.NavigateUrl = "qst_statistics_otherview.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = deptId;
            oNode.Type = "root";  
            return oNode;
        } 
    }
}
